// subpage/pages/combodetail/combodetail.js
import service from '../../../utils/request'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    combo: {},
    attr: [],
    current: 0,
    isPay: false,
    serverPhone: app.globalData.serverPhone,
    gunId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id: options.id,
      gunId:options.gunId
    })
    this.getDetail()
    this.getOption()
  },
  phoneCall(){
    wx.makePhoneCall({
      phoneNumber: this.data.serverPhone 
    })
  },
  getDetail (){
    let that = this
    let res = service('GET', '/personal/combo/activity/info/'+this.data.id).then(result => {
      console.log('套餐',result)
      that.setData({
        combo: result.data.info,
      })
    })
  },
  getOption (){
    let that = this
    let res = service('POST', '/personal/combo/pay/list', this.data.id).then(result => {
      console.log('套餐',result)
      that.setData({
        attr: result.data.list,
        current: result.data.list[0].id,
      })
    })
  },
  pay (){
    let that = this
    if(this.data.isPay){
      return false
    }
    that.setData({
      isPay: true
    })
    if(!this.data.current){
      wx.showToast({
        title: '请选择套餐类型',
      })
      return
    }
    let data = {
      id:  this.data.current,
      // takeEffect: 1,
      force: false,
      gunId: this.data.gunId
    }
    let res = service('POST', '/personal/combo/activity/buy', data).then(result => {
      console.log('购买',result)
      wx.showToast({
        title: result.data.msg,
        icon: 'none'
      })
      that.setData({
        isPay: false
      })
    })
  },
  chooseTab(e){
    let that = this
    this.setData({
      current: e.currentTarget.dataset.id
    })
  },
   //购买
   Buy(){
    let id = this.data.id
    let that = this
    let res = service('POST', '/combo/activity/buy',{
      id: id,
      force: false
    }).then(result => {
      console.log('套餐',result)
      if(result.data.code ==0){
        that.weiPay(JSON.parse(res.data.params))
       
      }else{
        wx.showToast({
          title: result.data.msg,
          icon: 'none'
        })
      }
      // that.setData({
      //   comboList: result.data.list,
      // })
    })
  },
    //发起支付
    weiPay(data) {
      let that= this
      wx.requestPayment({
        timeStamp: data.timeStamp,
        nonceStr: data.nonceStr,
        package: data.package,
        signType: data.signType,
        paySign: data.paySign,
        success(res) {
          console.log('购买成功', res)
          
        },
        fail(result) {
          console.log('用户取消了支付', result)
        }
      })
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})